package com.spzx.product.api.factory;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.api.RemoteProductService;
import com.spzx.product.domain.SkuPriceDTO;
import com.spzx.product.domain.SkuStockDTO;
import com.spzx.product.domain.vo.IndexVO;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.domain.SkuQuery;
import com.spzx.product.domain.vo.ProductSkuDetailsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RemoteProductFallbackFactory implements FallbackFactory<RemoteProductService>
{
    // 下面获取log对象的方式和 @Slf4j一样
    private static final Logger log = LoggerFactory.getLogger(RemoteProductFallbackFactory.class);
    /*
        远程调用的feign客户端
        降级类需要实现feign客户端接口，同时实现它所有的方法
     */
    //创建降级类对象的方法，可以使用feign客户端接口当作返回值类型
    @Override
    public RemoteProductService create(Throwable throwable)
    {
        log.error("商品服务调用失败:{}", throwable.getMessage());
        return new RemoteProductService(){
            @Override
            public R<List<SkuStockDTO>> checkSkusStock(List<SkuStockDTO> skuStockDTOs) {
                return  R.fail("检验库存调用失败:" + throwable.getMessage());
            }

            @Override
            public R<List<SkuPriceDTO>> checkSkusPrice(List<SkuPriceDTO> skuPriceDTOS) {
                return  R.fail("检验价格调用失败:" + throwable.getMessage());
            }

            @Override
            public R<ProductSku> getSkuById(long skuId) {
                return R.fail("根据skuid查询购物项商品详情失败"+throwable.getMessage());
            }

            @Override
            public R<IndexVO> listIndexVO() {
                return R.fail("获取热门数据失败:" + throwable.getMessage());
            }

            @Override
            public R<TableDataInfo> skuList(SkuQuery skuQuery) {
                return R.fail("获取商品列表失败:" + throwable.getMessage());
            }

            @Override
            public R<ProductSkuDetailsVO> getSkuDetailsVO(Long skuId) {
                return R.fail("获取商品详情数据失败:" + throwable.getMessage());
            }
        };
    }
}